﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class CustomerList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {         
            if (!IsPostBack)
            {
                BindCustomerList(1);
            }          
        }
        private void BindCustomerList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause += " and (CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or CustomerAddress like '%"+txtContractCodeBasic.Text.Trim()+"%')";
            }
            if (rbage.SelectedValue.ToString() == "1")
            { 
                Whereclause += " and v.CustomerID  is not null ";
            }
            if (rbage.SelectedValue.ToString() == "0")
            {
                Whereclause += " and v.CustomerID is null ";
            }
            if (rbsalesman.SelectedValue.ToString()=="1")
            {
                Whereclause += " and Salesman is not null ";
            }
            if (rbsalesman.SelectedValue.ToString() == "0")
            {
                Whereclause += " and Salesman is null ";
            }

            if (rbSalesModelType.SelectedValue.ToString() == "")
            {
                Whereclause += " and SalesModelType is null ";
            }
            else if (rbSalesModelType.SelectedValue.ToString() != "9")
            { Whereclause += " and SalesModelType ='" + rbSalesModelType.SelectedValue.ToString() + "'"; }

            if (rbSalesType.SelectedValue.ToString() == "")
            {
                Whereclause += " and SalesType is null ";
            }
            else if (rbSalesType.SelectedValue.ToString() != "9")
            { Whereclause += " and SalesType ='" + rbSalesType.SelectedValue.ToString() + "'"; }


            DataSynchroBll bll = new DataSynchroBll();
            int RecordCount = 0;
            //List<CtrtCustomerEntity> list = new SysUserBll().GetAll<CtrtCustomerEntity>(Whereclause, "CustomerCode asc", AspNetPager1.PageSize, pageindex, out RecordCount);
            DataTable dt = bll.QueryCustomerlist(Whereclause, "CustomerCode asc", AspNetPager1.PageSize, pageindex, out RecordCount);
            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindCustomerList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindCustomerList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindCustomerList(1);
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = "";
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                Whereclause += " and (CustomerName like '%" + txtContractCodeBasic.Text.Trim() + "%' or CustomerAddress like '%" + txtContractCodeBasic.Text.Trim() + "%')";
            }
            if (rbage.SelectedValue.ToString() == "1")
            {
                Whereclause += " and v.CustomerID  is not null ";
            }
            if (rbage.SelectedValue.ToString() == "0")
            {
                Whereclause += " and v.CustomerID is null ";
            }
            if (rbsalesman.SelectedValue.ToString() == "1")
            {
                Whereclause += " and Salesman is not null ";
            }
            if (rbsalesman.SelectedValue.ToString() == "0")
            {
                Whereclause += " and Salesman is null ";
            }

            if (rbSalesModelType.SelectedValue.ToString() == "")
            {
                Whereclause += " and SalesModelType is null ";
            }
            else if (rbSalesModelType.SelectedValue.ToString() != "9")
            { Whereclause += " and SalesModelType ='" + rbSalesModelType.SelectedValue.ToString()+"'"; }

            
            DataSynchroBll bll = new DataSynchroBll();

            DataTable dt = bll.ExportCustomerList(Whereclause, "CustomerName");

            Export.Export2Excel(dt, "客户列表（注意保密）", Page.Response);

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSynchroBll bll = new DataSynchroBll();
            bll.SynchroCustomer();
        }


    }
}
